"""
cron: */30 * * * *
new Env('百度热搜采集');
"""
import requests
import os
import re
import mysql.connector
from datetime import datetime
from bs4 import BeautifulSoup

def get_db_config():
    """获取数据库配置"""
    try:
        host = os.environ.get('HOST')
        user = os.environ.get('USER')
        password = os.environ.get('PASSWORD')
        database = os.environ.get('DATABASE')
        
        print("正在获取环境变量...")
        print(f"数据库地址：{host}")
        print(f"数据库用户名：{user}")
        print(f"数据库密码：{password}")
        print(f"数据库名称：{database}")
        
        if not all([host, user, password, database]):
            raise ValueError("环境变量不完整")
            
        return host, user, password, database
    except Exception as e:
        print(f"获取数据库配置失败: {str(e)}")
        raise

def get_baidu_hot_news():
    """获取百度热搜数据"""
    try:
        print("发送HTTP请求获取热点数据...")
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get('https://top.baidu.com/board?tab=realtime', headers=headers)
        response.raise_for_status()  # 检查请求是否成功
        
        soup = BeautifulSoup(response.text, 'html.parser')
        hot_items = soup.find_all('div', class_=re.compile(r'category-wrap'))
        
        if not hot_items:
            raise ValueError("未找到热搜数据")
            
        return hot_items
    except Exception as e:
        print(f"获取百度热搜失败: {str(e)}")
        raise

def save_to_database(connection, hot_items):
    """保存数据到数据库"""
    try:
        cursor = connection.cursor()
        
        # 设置要处理的ID范围（1-20用于百度热搜）
        id_list = list(range(1, 21))
        
        # 删除已有数据
        print("删除已有数据...")
        delete_query = "DELETE FROM ningm_news_list WHERE id IN ({})".format(", ".join(str(i) for i in id_list))
        cursor.execute(delete_query)
        
        # 保存新数据
        print("保存热搜数据到数据库...")
        for index, item in enumerate(hot_items):
            if index >= 20:  # 只保存前20条
                break
                
            try:
                title = item.find_all('div', class_=re.compile(r'c-single-text'))[0].text.strip()
                content = item.find_all('div', class_=re.compile(r'hot-desc'))[0].text.strip()
                url = item.find_all('a', class_=re.compile(r'title_'))[0].get('href', '')
                img = item.find_all('a', class_=re.compile(r'img-wrapper'))[0].get('href', '')
                
                print(f"正在保存第{index + 1}条数据: {title}")
                
                insert_query = """
                    INSERT INTO ningm_news_list 
                    (id, title, url, create_time, contant, img, type) 
                    VALUES (%s, %s, %s, %s, %s, %s, %s)
                """
                insert_values = (
                    id_list[index],
                    title,
                    url,
                    datetime.now(),
                    content,
                    img,
                    'baidu'
                )
                cursor.execute(insert_query, insert_values)
                
            except Exception as e:
                print(f"保存第{index + 1}条数据失败: {str(e)}")
                continue
        
        connection.commit()
        print(f"成功保存{min(len(hot_items), 20)}条数据")
        
    except Exception as e:
        print(f"数据库操作失败: {str(e)}")
        connection.rollback()
        raise
    finally:
        cursor.close()

def main():
    connection = None
    try:
        # 获取数据库配置
        host, user, password, database = get_db_config()
        
        # 连接数据库
        print("连接到MySQL数据库...")
        connection = mysql.connector.connect(
            host=host,
            user=user,
            password=password,
            database=database
        )
        
        # 获取热搜数据
        hot_items = get_baidu_hot_news()
        
        # 保存到数据库
        save_to_database(connection, hot_items)
        
        print("数据采集任务完成！")
        
    except Exception as e:
        print(f"程序执行失败: {str(e)}")
    finally:
        if connection:
            connection.close()
            print("数据库连接已关闭")

if __name__ == "__main__":
    main()
